﻿using com.guojin.mj.net.message.game;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class PlayerHeadPage : EventManager
{
    public override void InformationSetting()
    {
    }

    public RawImage headRawImg;
    public Text nameStr; 
    public Text totalScoreStr;
    public Image settleScoreBg;
    public Text settleScoreStr;
    public GameObject downTimeObj;
    public Button headBtn;

    public void InitPlayerHead(GameUserInfoRet gameUser)
    {
        DownloadHeadPhoto(gameUser.avatar);
        SetNameText(gameUser.userName);
        SetTotalScoreText(gameUser.totalScore);
    }

    public   void DownloadHeadPhoto(string url)
    {
        DownloadHeadTexture(headRawImg.gameObject.GetComponent<RawImage>(), url);
        StartCoroutine(CopyHeadRawImgToHeadTexture());
    }

    //开启协程等待下载完头像后赋值给headTexture,缓存到player类中
    IEnumerator CopyHeadRawImgToHeadTexture()
    {
        yield return new WaitUntil(() => headRawImg.texture != null);
        transform.GetComponent<Player>().headTexture = headRawImg.texture;
    }

    public  void SetNameText(string name)
    {
        SetText(nameStr, name);
    }

    public  void SetTotalScoreText(int toatalScore)
    {
        SetText(totalScoreStr, toatalScore.ToString());
    }

    public void SetSettleScore(bool show, int settleScore)
    {
        if (show)
        {
            settleScoreBg.gameObject.SetActive(true);
            SetSettleScoreText(settleScore);
            SetSettleScoreBg(settleScore);
        }
        else
        {
            settleScoreBg.gameObject.SetActive(false);
        }
    }

    private  void SetSettleScoreText(int settleScore)
    {
        if (settleScore >= 0)
        {
            SetText(settleScoreStr,"+"+ settleScore.ToString());
        }
        else
        {
            SetText(settleScoreStr, settleScore.ToString());
        }    
    }

    private void SetSettleScoreBg(int settleScore)
    {
        if (settleScore >= 0)
        {
            settleScoreBg.GetComponent<Image>().sprite = Resources.Load<Sprite>("UI/GameRoom/结算1");
        }
        else
        {
            settleScoreBg.GetComponent<Image>().sprite = Resources.Load<Sprite>("UI/GameRoom/结算0");
        }
    }

    public void SetDownTime(bool show, float  downTime)
    {
        if (show)
        {
            downTimeObj.SetActive(true);
            downTimeObj.GetComponent<DownTimePage>().SetDownTime(downTime);
        }
        else
        {
            downTimeObj.SetActive(false);
            downTimeObj.GetComponent<DownTimePage>().SetDownTime(0);
        }
    }

    public float  GetDownTime()
    {
        return  downTimeObj.GetComponent<DownTimePage>().GetDownTime();
    }

    public void HeadBtnClick()
    {
        Debug.Log("点击头像");

        UIManager.ChangeUI(UIManager.PageState.PlayerInfoPage, (GameObject obj) =>
        {
            obj.GetComponent<PlayerInfoPage>().InformationSetting();
            obj.GetComponent<PlayerInfoPage>().Init(gameObject.GetComponent<Player>());
        });
    }
}
